import react from "@vitejs/plugin-react";
import { codeInspectorPlugin } from "code-inspector-plugin";
import { resolve } from "path";
import { defineConfig } from "vite";
import tailwindcss from "@tailwindcss/vite";

let devProxyServer = "http://yps.aotbot.com:5230";
if (process.env.DEV_PROXY_SERVER && process.env.DEV_PROXY_SERVER.length > 0) {
  console.log(
    "Use devProxyServer from environment: ",
    process.env.DEV_PROXY_SERVER,
  );
  devProxyServer = process.env.DEV_PROXY_SERVER;
}

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    react(),
    tailwindcss(),
    codeInspectorPlugin({
      bundler: "vite",
    }),
  ],
  server: {
    host: "0.0.0.0",
    port: 3001,
    proxy: {
      "^/api": {
        target: devProxyServer,
        xfwd: true,
      },
      "^/memos.api.v1": {
        target: devProxyServer,
        xfwd: true,
      },
      "^/file": {
        target: devProxyServer,
        xfwd: true,
      },
    },
  },
  resolve: {
    alias: {
      "@/": `${resolve(__dirname, "src")}/`,
    },
  },
  build: {
    chunkSizeWarningLimit: 200,
    minify: "terser", // 你已经安装了 terser
    terserOptions: {
      compress: {
        drop_console: true, // 生产环境移除 console
        drop_debugger: true,
      },
    },
    rollupOptions: {
      output: {
        manualChunks: {
          "mermaid-vendor": ["mermaid"],
          "leaflet-vendor": ["leaflet", "react-leaflet"],
          "react-vendor": ["react", "react-dom"],
          "router-vendor": ["react-router-dom", "history"],
          "radix-vendor": [
            "@radix-ui/react-checkbox",
            "@radix-ui/react-dialog",
            "@radix-ui/react-dropdown-menu",
            "@radix-ui/react-label",
            "@radix-ui/react-popover",
            "@radix-ui/react-radio-group",
            "@radix-ui/react-select",
            "@radix-ui/react-separator",
            "@radix-ui/react-slot",
            "@radix-ui/react-switch",
            "@radix-ui/react-tooltip",
          ],
          "mobx-vendor": ["mobx", "mobx-react-lite"],
          "i18n-vendor": ["i18next", "react-i18next"],
          "utils-vendor": [
            "dayjs",
            "lodash-es",
            "clsx",
            "copy-to-clipboard",
            "uuid",
            "mime",
          ],
          "katex-vendor": ["katex", "@matejmazur/react-katex"],
          "graph-vendor": ["react-force-graph-2d"],
          "highlight-vendor": ["highlight.js"],
        },
      },
    },
  },
});
